package com.fs.system.college_admin.service;

import com.fs.system.college_admin.domain.Speciality;
import com.fs.system.college_admin.mapper.CollegeAdminSpecialityMapper;
import com.fs.system.common.ResponseData;
import com.fs.system.exception.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CollegeAdminSpecialityService {

    @Autowired
    private CollegeAdminSpecialityMapper collegeAdminSpecialityMapper;

    /**
     * 查询所有专业列表
     * @param speciality
     * @return
     */
    public List<Speciality> getAllSpeciality(Speciality speciality) {
        return collegeAdminSpecialityMapper.selectAllSpeciality(speciality);
    }

    public Speciality selectSpecialityById(Long specialityId) {
        return collegeAdminSpecialityMapper.selectSpecialityById(specialityId);
    }

    /**
     * 分页查询专业列表，根据collegeId查询
     * @param speciality
     * @param page
     * @param size
     * @return
     */
    public PageInfo<Speciality> selectSpeciality(Speciality speciality, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Speciality> gradeList = collegeAdminSpecialityMapper.selectAllSpecialityByCollege(speciality);
        return PageInfo.of(gradeList);
    }

    /**
     * 新增专业
     */
    public void addSpeciality(Speciality speciality) {
        //先判断是否存在
        Speciality dbspeciality = collegeAdminSpecialityMapper.selectByName(speciality);
        if(Objects.nonNull(dbspeciality)){
            throw new CustomerException("500","新增失败！专业名称已重复");
        }
        speciality.setDeleted(false);
        speciality.setCreateTime(LocalDateTime.now());
        collegeAdminSpecialityMapper.addSpeciality(speciality);
    }

    /**
     * 删除方法
     * @param specialityId
     * @param updateBy
     */
    public void deleteSpeciality(Long specialityId, String updateBy) {
        collegeAdminSpecialityMapper.deleteSpeciality(specialityId,updateBy,LocalDateTime.now());
    }

    /**
     * 修改专业信息
     * @param speciality
     */
    public void updateSpeciality(Speciality speciality) {
        //不准修改成已有的专业名称
        Speciality dbspeciality = collegeAdminSpecialityMapper.selectByName(speciality);
        if(!Objects.isNull(dbspeciality)){
            if(!Objects.equals(dbspeciality.getSpecialityId(), speciality.getSpecialityId())){
                throw new CustomerException("500","修改失败！专业名称已重复");
            }
        }
        speciality.setUpdateTime(LocalDateTime.now());
        collegeAdminSpecialityMapper.updateSpeciality(speciality);
    }
}
